package com.pucheng.bracelet.ui.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import android.widget.CheckBox
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.ByteUtils
import com.pucheng.bracelet.app.util.ByteUtils.getIntToIAsBit
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.data.bean.WeekBean
import com.pucheng.bracelet.ui.adapter.ClockData
import kotlinx.android.synthetic.main.dialog_week_day_select.*


class WeekDaySelectDialog(mContext: Context, var weekChecks: Int = 0) :
    Dialog(mContext, R.style.bottomDialogStyle) {

    var onWeekRepeatCallback:OnWeekRepeatCallback? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val window = this.window
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.WRAP_CONTENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.setGravity(Gravity.BOTTOM)
        window!!.attributes = params

        setContentView(R.layout.dialog_week_day_select)

        tv_cancel.click {
            dismiss()
        }
        tv_save.click {
            dismiss()
            if(onWeekRepeatCallback !=null){
                onWeekRepeatCallback!!.onWeekRepeat(weekChecks)
            }
        }
        val weekDatas = context.resources.getStringArray(R.array.week)
        val weekList = arrayListOf<WeekBean>()


        for (i in weekDatas.indices) {
            weekList.add(WeekBean(weekDatas[i], getIntToIAsBit(weekChecks, i)))
        }
        val mAdapter =
            object : BaseQuickAdapter<WeekBean, BaseViewHolder>(R.layout.item_weeks, weekList) {
                override fun convert(holder: BaseViewHolder, item: WeekBean) {
                    holder.setText(R.id.tv_week, item.weekStr)
                    val cbWeek = holder.getView<CheckBox>(R.id.cb_week)
                    cbWeek.isChecked = item.checked
                    cbWeek.setOnCheckedChangeListener { _, isChecked ->
                        val index = holder.adapterPosition
                        weekChecks = if (isChecked) {
                            ByteUtils.intAsIndexTo1(weekChecks,index )
                        } else {
                            ByteUtils.intAsIndexTo0(weekChecks, index)
                        }
                        LogUtils.e("${Integer.toHexString(weekChecks)} weekChecks = ${Integer.toBinaryString(weekChecks)}")
                    }

                }

            }
        rv_week.adapter = mAdapter
    }

    fun setOnWeekRepeatCallback(onWeekRepeatCallback: OnWeekRepeatCallback): WeekDaySelectDialog {
        this@WeekDaySelectDialog.onWeekRepeatCallback = onWeekRepeatCallback
        return this@WeekDaySelectDialog
    }

}
interface OnWeekRepeatCallback{
    fun onWeekRepeat(weekRepeat:Int)
}